<style lang="less">
  @import './login.less';
</style>
<template>
  <div class="login">
    <div class="login-con">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>欢迎登录</span>
        </div>
        <div class="form-con">
          <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm"
                   label-position="left">
            <el-form-item prop="username">
              <el-input name="username" type="text" v-model="loginForm.username" autoComplete="on"
                        placeholder="用户名" clearable prefix-icon="el-icon-lemone-user1"/>
            </el-form-item>
            <el-form-item prop="password">
              <el-input name="password" type="password" @keyup.enter.native="handleLogin" v-model="loginForm.password"
                        autoComplete="on"
                        placeholder="密码" clearable prefix-icon="el-icon-lemone-mima"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
                登录
              </el-button>
            </el-form-item>
            <p class="login-tip">© {{new Date().getFullYear()}} XXX科技提供技术支持</p>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
  import {isvalidUsername} from '@/common/validate'
  import {desEncrypt} from '@/common/encrypt'

  export default {
    name: 'login',
    data() {
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          username: '',
          password: ''
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', message: '用户名不能为空'}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password'
      }
    },
    methods: {
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            let t = new Date().getTime() + ''
            this.$store.dispatch('Login', {
              t: t,
              username: this.loginForm.username,
              password: desEncrypt(this.loginForm.password, t)
            }).then(() => {
              this.loading = false
              this.$router.push({path: '/dashboard'})
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    }
  }
</script>

<style>
</style>
